﻿using FBroSharp;
using FBroSharp.Const;
using FBroSharp.DataType;
using FBroSharp.Lib;
using FBroSharp.Value;
using FBroSharp.VIP;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VIP指纹测试
{
    public class BrowserDis
    {

        public static string[] fontList = { "Arial", "Arial Black", "Bahnschrift", "Calibri", "Cambria", "Cambria Math", "Candara", "Comic Sans MS", "Consolas", "Constantia", "Corbel", "Courier New", "Ebrima", "Franklin Gothic Medium", "Gabriola", "Gadugi", "Georgia", "HoloLens MDL2 Assets", "Impact", "Ink Free", "Javanese Text", "Leelawadee UI", "Lucida Console", "Lucida Sans Unicode", "Malgun Gothic", "Marlett", "Microsoft Himalaya", "Microsoft JhengHei", "Microsoft New Tai Lue", "Microsoft PhagsPa", "Microsoft Sans Serif", "Microsoft Tai Le", "Microsoft YaHei", "Microsoft Yi Baiti", "MingLiU-ExtB", "Mongolian Baiti", "MS Gothic", "MV Boli", "Myanmar Text", "Nirmala UI", "Palatino Linotype", "Segoe MDL2 Assets", "Segoe Print", "Segoe Script", "Segoe UI", "Segoe UI Historic", "Segoe UI Emoji", "Segoe UI Symbol", "SimSun", "Sitka", "Sylfaen", "Symbol", "Tahoma", "Times New Roman", "Trebuchet MS", "Verdana", "Webdings", "Wingdings", "Yu Gothic", "Al Bayan", "Al Nile", "Al Tarikh", "American Typewriter", "American Typewriter Condensed", "Andale Mono", "Apple Braille", "Apple Braille Outline 6 Dot", "Apple Braille Outline 8 Dot", "Apple Braille Pinpoint 6 Dot", "Apple Braille Pinpoint 8 Dot", "Apple Chancery", "Apple Color Emoji", "Apple SD Gothic Neo", "Apple SD Gothic Neo Extra", "Apple Symbols", "AppleGothic", "AppleMyungjo", "Arial", "Arial Black", "Arial Hebrew", "Arial Hebrew Scholar", "Arial Narrow", "Arial Rounded MT", "Arial Unicode MS", "Avenir Black", "Avenir Book", "Avenir", "Avenir Next", "Avenir Next Condensed", "Avenir Roman", "Ayuthaya", "Baghdad", "Bangla MN", "Bangla Sangam MN", "Baskerville", "Beirut", "Big Caslon", "Bodoni 72", "Bodoni 72 Book", "Bodoni 72 Oldstyle", "Bodoni 72 Oldstyle Book", "Bodoni 72 Smallcaps Book", "Bodoni Ornaments", "Bradley Hand", "Brush Script MT", "Chalkboard", "Chalkboard SE", "Chalkduster", "Charter Black", "Charter", "Charter Roman", "Cochin", "Copperplate", "Corsiva Hebrew", "Courier", "Courier New", "DIN Alternate", "DIN Condensed", "Damascus", "Damascus Semi", "DecoType Naskh", "Devanagari MT", "Devanagari Sangam MN", "Didot", "Diwan Kufi", "Diwan Thuluth", "Euphemia UCAS", "Farah", "Farisi", "Futura", "Futura Condensed Extra", "Futura Condensed", "GB18030 Bitmap", "Galvji", "Geeza Pro", "Geneva", "Georgia", "Gill Sans", "Gujarati MT", "Gujarati Sangam MN", "Gurmukhi MN", "Gurmukhi MT", "Gurmukhi Sangam MN", "Heiti SC", "Heiti TC", "Helvetica", "Helvetica Neue", "Helvetica Neue Condensed Black", "Helvetica Neue Condensed", "Herculanum", "Hiragino Maru Gothic ProN W4", "Hiragino Mincho ProN W3", "Hiragino Mincho ProN W6", "Hiragino Sans GB W3", "Hiragino Sans GB W6", "Hiragino Sans W0", "Hiragino Sans W1", "Hiragino Sans W2", "Hiragino Sans W3", "Hiragino Sans W4", "Hiragino Sans W5", "Hiragino Sans W6", "Hiragino Sans W7", "Hiragino Sans W8", "Hiragino Sans W9", "Hoefler Text", "Hoefler Text Black", "Hoefler Text Ornaments", "ITF Devanagari", "ITF Devanagari Book", "ITF Devanagari Marathi", "ITF Devanagari Marathi Book", "Impact", "InaiMathi", "Kailasa", "Kannada MN", "Kannada Sangam MN", "Kefa", "Khmer MN", "Khmer Sangam MN", "Kohinoor Bangla", "Kohinoor Devanagari", "Kohinoor Gujarati", "Kohinoor Telugu", "Kokonor", "Krungthep", "KufiStandardGK", "Lao MN", "Lao Sangam MN", "Lucida Grande", "Luminari", "Malayalam MN", "Malayalam Sangam MN", "Marker Felt", "Marker Felt Wide", "Menlo", "Microsoft Sans Serif", "Mishafi Gold", "Mishafi", "Monaco", "Mshtakan", "MuktaMahee", "Muna Black", "Muna", "Myanmar MN", "Myanmar Sangam MN", "Nadeem", "New Peninim MT", "New Peninim MT Bold Inclined", "New Peninim MT Inclined", "Noteworthy", "Noto Nastaliq Urdu", "Noto Sans Javanese", "Noto Sans Kannada Black", "Noto Sans Kannada", "Noto Sans Kannada Extra", "Noto Sans Myanmar Black", "Noto Sans Myanmar", "Noto Sans Myanmar Extra", "Noto Sans Oriya", "Optima", "Oriya MN", "Oriya Sangam MN", "PT Mono", "PT Sans", "PT Sans Caption", "PT Sans Narrow", "PT Serif", "PT Serif Caption", "Palatino", "Papyrus", "Papyrus Condensed", "Phosphate Inline", "Phosphate Solid", "PingFang HK", "PingFang SC", "PingFang TC", "Plantagenet Cherokee", "Raanana", "Rockwell", "STIXGeneral-", "STIXIntegralsD-", "STIXIntegralsSm-", "STIXIntegralsUp-", "STIXIntegralsUpD-", "STIXIntegralsUpSm-", "STIXNonUnicode-", "STIXSizeFiveSym-", "STIXSizeFourSym-", "STIXSizeOneSym-", "STIXSizeThreeSym-", "STIXSizeTwoSym-", "STIXVariants-", "STSong", "Sana", "Sathu", "Savoye LET Plain:1.0", "Shree Devanagari 714", "SignPainter-HouseScript", "Silom", "Sinhala MN", "Sinhala Sangam MN", "Skia Black", "Skia Black Condensed", "Skia Black Extended", "Skia", "Skia Condensed", "Skia Extended", "Skia Light Condensed", "Skia Light Extended", "Snell Roundhand", "Snell Roundhand Black", "Songti SC Black", "Songti SC", "Songti TC", "Sukhumvit Set", "Sukhumvit Set Semi", "Sukhumvit Set Text", "Symbol", "Tahoma", "Tamil MN", "Tamil Sangam MN", "Telugu MN", "Telugu Sangam MN", "Thonburi", "Times", "Times New Roman", "Times Roman", "Trattatello", "Trebuchet MS", "Verdana", "Waseem", "Webdings", "Wingdings", "Wingdings 2", "Wingdings 3", "Zapf Dingbats", "Zapfino", "Droid Sans", "Droid Serif", "Droid Sans Mono", "times new", "goudy" };
        public static string[] mimeTypesList = { "application/futuresplash", "application/msexcel", "application/mspowerpoint", "application/msword", "application/msword-template", "application/pdf", "application/vnd.ces-quickpoint", "application/vnd.ces-quicksheet", "application/vnd.ces-quickword", "application/vnd.ms-excel", "application/vnd.ms-excel.sheet.macroEnabled.12", "application/vnd.ms-excel.sheet.macroenabled.12", "application/vnd.ms-powerpoint", "application/vnd.ms-powerpoint.presentation.macroEnabled.12", "application/vnd.ms-powerpoint.presentation.macroenabled.12", "application/vnd.ms-word", "application/vnd.ms-word.document.12", "application/vnd.ms-word.document.macroEnabled.12", "application/vnd.ms-word.document.macroenabled.12", "application/vnd.msword", "application/vnd.openxmlformats-officedocument.presentationml.presentation", "application/vnd.openxmlformats-officedocument.presentationml.template", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "application/vnd.openxmlformats-officedocument.spreadsheetml.template", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "application/vnd.openxmlformats-officedocument.wordprocessingml.template", "application/vnd.presentation-openxml", "application/vnd.presentation-openxmlm", "application/vnd.spreadsheet-openxml", "application/vnd.wordprocessing-openxml", "application/x-google-chrome-pdf", "application/x-nacl", "application/x-pnacl", "application/x-shockwave-flash", "text/csv" };

        //创建浏览器
        public static void CreatBrowser(string title, string url, IntPtr parent_window, int x, int y, int width, int height)
        {
            //开始创建浏览器
            FBroSharpWindowsInfo windows_info = new FBroSharpWindowsInfo
            {
                parent_window = parent_window,
                x = x,
                y = y,
                width = width,
                height = height,
                window_name = title
            };

            //创建浏览器事件回调类
            BrowserEvent browser_event = new BrowserEvent();

            //设置运行环境
            //FBroSharpRequestContextSet contextSet = new FBroSharpRequestContextSet();
            //contextSet.cache_path = Directory.GetCurrentDirectory() + "\\Cache\\other cache\\"; ;

            //FBroSharpRequestContext request_context = (FBroSharpRequestContext)FBroSharpRequestContext.CreateContext(contextSet);
            //Console.WriteLine("缓存目录:" + request_context.GetCachePath());

            //设置不需要的事件
            //FBroSharpEventDisableControl event_contrl = new FBroSharpEventDisableControl();
            //event_contrl.disableGetAudioHandler = true;


            //设置传递参数
            //FBroSharpDictionaryValue extra_info = new FBroSharpDictionaryValue();//.Creat();
            //extra_info.SetString("传参测试", "这是创建浏览器的时候传递过来的参数");

            if (FBroSharpControl.CreatBrowser(url, windows_info, default, default /*request_context*/, default/*extra_info*/, browser_event, default/*event_contrl*/))
                Console.WriteLine("true");
        }



        public static void ChangeFingerPrint(IFBroSharpBrowser browser)
        {
            //获取vip功能控制器
            IFBroSharpVIPControl vipControl = browser.GetVIPControl();
            if (vipControl == null || !vipControl.IsValid)
                return;

            //获取指纹控制器
            IFBroSharpFingerPrintControl fingerPrintControl = vipControl.GetFingerPrintControl();
            if (fingerPrintControl == null || !fingerPrintControl.IsValid)
                return;


            fingerPrintControl.SetVirProductSub("20030107");
            fingerPrintControl.SetVirVendor("Fbrowser Inc.");
            //fingerPrintControl.SetVirVendorSub("1.0");
            fingerPrintControl.SetVirUserAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.5304.88 Safari/537.36 Core/1.70.3870.400 QQBrowser/10.8.4405.400",
            "zh-CN,zh;en-EN", "Win64");

            fingerPrintControl.SetVirKernel(99);

            fingerPrintControl.SetVirLanguages("en-EN");
            fingerPrintControl.SetVirAppCodeName("F点浏览器");
            fingerPrintControl.SetVirAppName("Fbrowser");
            fingerPrintControl.SetVirAppVersion("5.0");
            fingerPrintControl.SetVirProduct("Fbrowser");
            fingerPrintControl.SetVirHardwareConcurrency((new Random()).Next(8, 16));
            fingerPrintControl.SetVirCookieEnabled(true);
            fingerPrintControl.SetVirDeviceMemory(32);

            //{//随机模式，不能和下面的定值模式一起使用

            //    //用当前时间戳来当随机种子
            //    TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            //    int srand = (int)ts.TotalSeconds;
            //    fingerPrintControl.SetCanvasFingerPrintRandom(1, 16, srand);
            //    fingerPrintControl.SetWebGLFingerPrintRandom(1, 100, srand);
            //    fingerPrintControl.SetAudioFingerPrintRandom(100, 1000, srand);
            //}


            {//定值模式，不能和上面的随机模式一起使用
                var r = new Random();
                //两个噪点，x:y:r:g:b:a值都是随机
                string canvasTemp = r.Next(0, 1000) + ":" + r.Next(0, 1000) + ":" + r.Next(0, 255) + ":" + r.Next(0, 255) + ":" + r.Next(0, 255) + ":" + r.Next(0, 255);
                canvasTemp += "," + r.Next(0, 1000) + ":" + r.Next(0, 1000) + ":" + r.Next(0, 255) + ":" + r.Next(0, 255) + ":" + r.Next(0, 255) + ":" + r.Next(0, 255);
                fingerPrintControl.SetCanvasFingerPrintConstant(canvasTemp);


                //定值两个噪点，x:y都是随机值
                string webglTemp = ((double)(r.Next(0, 200)) / 200 - 1).ToString() + ":" + ((double)(r.Next(0, 200)) / 200 - 1).ToString();
                webglTemp += "," + ((double)(r.Next(0, 200)) / 200 - 1).ToString() + ":" + ((double)(r.Next(0, 200)) / 200 - 1).ToString();
                fingerPrintControl.SetWebGLFingerPrintConstant(webglTemp);

                
                //随机100到200个点
                string audioTemp = r.Next(100, 9999) + ":" + r.Next(100, 999);
                for (int i = 0; i < r.Next(100, 200); i++)
                {
                    audioTemp += "," + r.Next(100, 9999) + ":" + r.Next(100, 9999);
                }
                fingerPrintControl.SetAudioFingerPrintConstant(audioTemp);
            }


            {//插件指纹信息设置

                //插件信息JSON格式：“{"data":[{"name":"1","filename":"9521123filename","description":"178123description","may_use_external_handler":"true","mimeinfo":[{"mime_types":"application/vnd.openxmlformats-officedocument.wordprocessingml.template","description":"","suffixes":"txt"}]},{"name":"2","filename":"9657123filename","description":"315123description","may_use_external_handler":"true","mimeinfo":[{"mime_types":"application/vnd.ms-excel","description":"","suffixes":"txt"}]},{"name":"3","filename":"5949123filename","description":"634123description","may_use_external_handler":"true","mimeinfo":[{"mime_types":"application/msexcel","description":"","suffixes":"txt"}]}]}”
                var value = FBroSharpValue.Creat();
                var dicValue = FBroSharpDictionaryValue.Creat();
                var list = FBroSharpListValue.Creat();
                //随机插件个数1到4个
                int plusCount = (new Random()).Next(1, 4);
                list.SetSize(plusCount);
                //循环写入插件信息
                for (int i = 0; i < plusCount; i++)
                {
                    var childDicValue = FBroSharpDictionaryValue.Creat();
                    //只是测试就用i作为插件名
                    childDicValue.SetString("name", i.ToString());
                    childDicValue.SetString("filename", "filename" + i.ToString());
                    childDicValue.SetString("description", "description" + i.ToString());
                    childDicValue.SetString("may_use_external_handler", "true");
                    var mimeinfoList = FBroSharpListValue.Creat();

                    //随机mime个数1到3个
                    int mimeinfoCount = (new Random()).Next(1, 3);
                    mimeinfoList.SetSize(mimeinfoCount);
                    for (int j = 0; j < mimeinfoCount; j++)
                    {
                        var mimeinfoDicValue = FBroSharpDictionaryValue.Creat();

                        mimeinfoDicValue.SetString("mime_types", mimeTypesList[(new Random()).Next(0, mimeTypesList.Length - 1)]);
                        mimeinfoDicValue.SetString("description", "");
                        mimeinfoDicValue.SetString("suffixes", "txt");

                        mimeinfoList.SetDictionary(i, mimeinfoDicValue);
                    }

                    childDicValue.SetList("mimeinfo", mimeinfoList);
                    list.SetDictionary(i, childDicValue);
                }
                dicValue.SetList("data", list);
                value.SetDictionary(dicValue);
                //将生成为json数据输出为文本
                string pluginsStr = FBroSharpControl.WriteJSON(value, FBroSharp.Const.FBroSharpJSONParserType.RFC);
                fingerPrintControl.SetPlugins(2, pluginsStr);
            }


            fingerPrintControl.SetVirJavaEnabled(true);
            fingerPrintControl.SetVirWebdriver(false);
            fingerPrintControl.SetVirOnLine(true);


            fingerPrintControl.SetVirCanvas2DFontFingerprint((double)((new Random()).Next(1, 2000)) / 1000 - 1);


            //将内置清单随机选择几个字体清单当作需要修改的字体传递给内核
            string fontStr = "";
            for (int i = 0; i < (new Random()).Next(10, fontList.Length); i++)
            {
                fontStr += fontList[(new Random()).Next(0, fontList.Length - 1)] + ",";
            }
            fingerPrintControl.SetVirCSSFontFingerprint(fontStr, (new Random()).Next(0, 100), (new Random()).Next(0, 100));


            fingerPrintControl.SetVirScreenXAndY(0, 0);

            //分辨率，先高在宽
            fingerPrintControl.SetVirScreenHeightAndWidth(1080, 1920);

            fingerPrintControl.SetVirScreenavailHeightAndWidth(1080 - 40, 1920);

            int depth = (new Random()).Next(0, 100);
            fingerPrintControl.SetVirScreencolorDepth(depth);
            fingerPrintControl.SetVirScreenpixelDepth(depth);

            double pixelRatio = ((double)(new Random()).Next(1, 199)) / 100;
            fingerPrintControl.SetVirDevicePixelRatio(pixelRatio);

            string[] vendor = { "Google Inc.", "Firfox Inc.", "Windows Inc.", "FBrowser Inc.", "Test1 Inc.", "Test2 Inc.", "Test3 Inc." };
            fingerPrintControl.SetVirWebglvendor(vendor[(new Random()).Next(0, vendor.Length - 1)]);


            fingerPrintControl.SetVirWebglrenderer("ANGLE (NVIDIA GeForce GTX" + (new Random()).Next(960, 4999) + "Ti Direct3D11 vs_5_0 ps_5_0)");

            fingerPrintControl.SetVirRectFingerprint((new Random()).Next(-100, 100), (new Random()).Next(-100, 100), (new Random()).Next(-100, 100), (new Random()).Next(-100, 100));

            fingerPrintControl.SetVirWebrtcIP("23.26.139.92", "192.168.1.100", "924ce16d-52c7-42a3-a29d-b78004f9e534234.local");

            fingerPrintControl.SetVirTimeZone(-10, 0, "美国", "America/Los_Angeles");

            fingerPrintControl.SetTouchEventEmulationEnabled(true, 1000);


            fingerPrintControl.SetVirBatteryManagerCharging(true);
            fingerPrintControl.SetVirBatteryManagerChargingTime(56.6);
            fingerPrintControl.SetVirBatteryManagerDischargingTime(60);
            fingerPrintControl.SetVirBatteryManagerLevel((double)((new Random()).Next(0, 100)) / 100);

            {//媒体设备虚拟
                List<FBroSharpAudioInputData> indata = new List<FBroSharpAudioInputData>();
                var item = new FBroSharpAudioInputData
                {
                    //这个值除了设置成default和communications为，设置的ID值其实是硬件的ID，不是JS通过命令显示的ID，这个ID和下面的分组ID一样，可以在你的硬件驱动里面取查找
                    deviceId = "default",
                    label = " - 扬声器 你好 (High Definition Audio Device)",
                    groupId = "{0.0.0.00000000}.{cfd553c5-5384-4eff-b51b-928f973cad88}"
                };
                indata.Add(item);

                var item1 = new FBroSharpAudioInputData
                {
                    //这个值除了设置成default和communications为，设置的ID值其实是硬件的ID，不是JS通过命令显示的ID，这个ID和下面的分组ID一样，可以在你的硬件驱动里面取查找
                    deviceId = "communications",
                    label = "Audio Device (High Definition Audio Device)",
                    groupId = "{0.0.0.00000000}.{cfd553c5-5384-4eff-b51b-928f973cad89"
                };
                indata.Add(item1);

                //设置0为清空
                fingerPrintControl.SetVirAudioInput(0, indata);
                fingerPrintControl.SetVirVideoInput(0, default);
                //设置2为覆盖
                fingerPrintControl.SetVirAudioOutput(2, indata);
            }

            fingerPrintControl.SetVirViewport(0, 0, 1080, 1920);

            //………………………………
            fingerPrintControl.SetSSLCipher(FBroSharpTLSVersion.NULL, FBroSharpTLSVersion.NULL, "ALL:!AES128");


            fingerPrintControl.SetVirLongitudeAndLatitude(104.144464, 30.7371, 231);


            IFBroSharpAdvancedControl advancedControl = vipControl.GetAdvancedControl();

            if (advancedControl == null || !advancedControl.IsValid)
                return;

            advancedControl.SetVirisTrusted(true);


            //禁用网页使用debugger
            advancedControl.SetDisableDebugger(true);
            advancedControl.SetDisableConsoleDebug(true);

            //fingerPrintControl.SetEmitTouchEventsForMouse(bool enable, int configuration);

            fingerPrintControl.SetVirOrientation(FBroSharpOrientationType.NULL,0);

            {
                List<FBroSharpSpeechSynthesisVoiceData> voicesListData = new List<FBroSharpSpeechSynthesisVoiceData>();

                FBroSharpSpeechSynthesisVoiceData voicesData = new FBroSharpSpeechSynthesisVoiceData
                {
                    voiceURI = "Microsoft Huihui - Chinese (Simplified, PRC)",
                    name = "Microsoft Huihui - Chinese (Simplified, PRC)",
                    lang = "zh-CN",
                    isDefault = true,
                    isLocalService = true
                };
                voicesListData.Add(voicesData);
                fingerPrintControl.SetVirSpeechSynthesisVoices(voicesListData);
            }
            
        }

    }
}
